home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_10_02 / 1002097b < prev    next >
Text File  |  1991-01-13  |  3KB  |  81 lines

  1.  
  2.  
  3. /*      RECUR is a recursive descent subroutine to search
  4.                 directories and subs    with wildcard and path
  5.                 capabilities -> then pass the path name
  6.                 to a user supplied function called "subfunc".
  7.                 2 switches are presently supported: totals
  8.                 and subs for subdirectories.
  9. */
  10.  
  11. #include <dos.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <malloc.h>
  15. #include <string.h>
  16.  
  17. #define MAX_SUB 128
  18.  
  19. extern  void subfunc(char *path);
  20.  
  21. int recurs(char *path, int subs, int totals)
  22. {
  23. struct find_t *fib;
  24. char *drive, *dir, *fname, *ext;
  25. char *tmppath, *tmpdir;
  26. char (*subfifoptr)[_MAX_FNAME];
  27. char *subfifo;
  28. int total = 0;
  29.                         /* malloc so we don't blow up the stack */
  30. fib = malloc(sizeof(struct find_t));
  31. drive = malloc(_MAX_DRIVE);
  32. dir = malloc(_MAX_DIR);
  33. fname = malloc(_MAX_FNAME);
  34. ext = malloc(_MAX_EXT);
  35. tmpdir = malloc(_MAX_DIR);
  36. tmppath = malloc(_MAX_PATH);
  37.  
  38. if (subs)
  39.         {
  40. /* if subs switch - do subdirectories first, RECURSIVELY */
  41.         subfifo = *subfifoptr =
  42.                                 (char *)malloc(_MAX_FNAME * MAX_SUB);
  43.         _splitpath(path, drive, dir, fname, ext);
  44.         _makepath(tmppath, drive, dir, "*", "*");
  45.         if (!_dos_findfirst(tmppath, _A_SUBDIR, fib))
  46.                 do
  47.                         {
  48.                         if (fib->name[0] != '.' && fib->attrib &
  49.                                                                          _A_SUBDIR)
  50.                                 strcpy(*subfifoptr++, fib->name);
  51.                         } while (!_dos_findnext(fib) && *subfifoptr <
  52.                                          &subfifo[(_MAX_FNAME - 1) * MAX_SUB]);
  53.         **subfifoptr = NULL;            /* terminate fifo */
  54.         *subfifoptr = subfifo;  /* reset fifo pointer */
  55.         while(**subfifoptr)             /* while not at the end */
  56.                 {
  57.                 strcpy(tmpdir, dir);
  58.                 _makepath(tmppath, drive, 
  59.                                 strcat(tmpdir, *subfifoptr++), fname, ext);
  60.                 total += recurs(tmppath, subs, totals);
  61.                 }
  62.         free(subfifo);
  63.         }
  64.  
  65. if (!_dos_findfirst(path, _A_NORMAL, fib))
  66.         do
  67.                 {
  68.                 _splitpath(path, drive, dir, fname, ext);
  69.                 strcpy(tmppath, drive);
  70.                 strcat(tmppath, dir);
  71.                 subfunc(strcat(tmppath, fib->name));
  72.                 total++;
  73.                 } while (!_dos_findnext(fib));
  74. if (totals)
  75.         printf("\ntotal = %d\t%s files\n", total, path);
  76.  
  77. free(fib), free(drive), free(dir), free(fname);
  78. free(ext), free(tmpdir), free(tmppath);
  79. return total;
  80. }
  81.